﻿using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using System;
using System.Text;
using System.Threading.Tasks;

namespace dotnetty_client2
{
    internal class NettyHandler : ChannelHandlerAdapter
    {
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            if (message is IByteBuffer buffer)
            {
                //buffer.ReadChar
                StringBuilder sb = new StringBuilder();
                //while (buffer.IsReadable())
                //{
                //    sb.Append(buffer.ReadByte().ToString("X2"));
                //}
                

                var str = buffer.ReadString(buffer.ReadableBytes, Encoding.UTF8);
                Console.WriteLine($"RECIVED FROM SERVER : {str}");
            }
        }
        public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
        {
            Console.WriteLine("Exception: " + exception);
            context.CloseAsync();
        }

        public override Task WriteAsync(IChannelHandlerContext context, object message)
        {
            return base.WriteAsync(context, message);
        }
    }
}
